package com.xbongbong.paas.service.impl;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasMenuEntity;
import com.xbongbong.paas.enums.MenuTypeEnum;
import com.xbongbong.paas.log.MongoLogHelp;
import com.xbongbong.paas.log.constant.LogMemoConstant;
import com.xbongbong.paas.model.PaasMenuModel;
import com.xbongbong.paas.pojo.dto.MenuFormAddDTO;
import com.xbongbong.paas.pojo.dto.MenuGroupAddDTO;
import com.xbongbong.paas.service.ProMenuService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.menu.enums.JXCMenuEnum;
import com.xbongbong.pro.menu.pojo.dto.MenuFormAddBatchDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuGroupAddBatchDTO;
import com.xbongbong.pro.menu.pojo.vo.MenuFormAddBatchVO;
import com.xbongbong.pro.menu.pojo.vo.MenuGroupAddBatchVO;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/10/28 13:52
 */
@Service("proMenuService")
public class ProMenuServiceImpl implements ProMenuService {

    private static final Logger LOG = LoggerFactory.getLogger(ProMenuServiceImpl.class);

    @Resource
    private PaasMenuModel paasMenuModel;
    @Resource
    private MongoLogHelp mongoLogHelp;

    @Override
    public MenuGroupAddBatchVO addBatchGroupMenu(MenuGroupAddBatchDTO menuGroupAddBatchDTO) throws XbbException {
        MenuGroupAddBatchVO menuGroupAddBatchVO = new MenuGroupAddBatchVO();
        try {
            String corpid = menuGroupAddBatchDTO.getCorpid();
            Map<String, Object> params = BeanUtil.convertBean2Map(menuGroupAddBatchDTO, true);
            params.put("orderByStr", "sort DESC ");
            Integer count = paasMenuModel.getEntitysCount(params);
            if(count == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.MENU_NOT_EXIST);
            }
            List<PaasMenuEntity> menuList = new ArrayList<>();
            for (MenuGroupAddDTO item : menuGroupAddBatchDTO.getMenuList()) {
                long now = DateUtil.getInt();
                PaasMenuEntity paasMenuEntity = new PaasMenuEntity();
                BeanUtil.copyProperties(item, paasMenuEntity);
                paasMenuEntity.setCorpid(corpid);
                count = count + 1;
                paasMenuEntity.setSort(count);
                paasMenuEntity.setCreatorId(menuGroupAddBatchDTO.getUserId());
                paasMenuEntity.setAddTime(now);
                paasMenuEntity.setUpdateTime(now);
                if (Objects.isNull(paasMenuEntity.getDistributorMark())) {
                    paasMenuEntity.setDistributorMark(DistributorMarkEnum.OTHER.getCode());
                }
                menuList.add(paasMenuEntity);
            }
            List<PaasMenuEntity> resultList = paasMenuModel.insertBatch(menuList);
            if (Objects.isNull(resultList)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            menuGroupAddBatchVO.setMenuList(resultList);
            String userName = menuGroupAddBatchDTO.getLoginUserName();
            // 记录日志
            if (Objects.equals(menuGroupAddBatchDTO.getCompanyInit(), 0)) {
                String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_ADD_MENU), userName, "");
                mongoLogHelp.buildLog(corpid, menuGroupAddBatchDTO.getUserId(), userName, OperateModuleTypeEnum.PAAS_MENU, OperateTypeEnum.NEW, "", "", memo, menuGroupAddBatchDTO.getHttpHeader());
            }

        } catch (XbbException e) {
            throw e;
        }  catch (Exception e) {
            LOG.error("menu数据库批量插入菜单出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return menuGroupAddBatchVO;
    }

    @Override
    public MenuFormAddBatchVO addBatchFormMenu(MenuFormAddBatchDTO menuFormAddBatchDTO) throws XbbException {
        MenuFormAddBatchVO menuFormAddBatchVO = new MenuFormAddBatchVO();
        try {
            String corpid = menuFormAddBatchDTO.getCorpid();
            Map<String, Object> params = BeanUtil.convertBean2Map(menuFormAddBatchDTO, true);
            params.put("orderByStr", "sort DESC ");
            List<Integer> typeIn = new ArrayList<>();
            typeIn.add(MenuTypeEnum.FORM.getType());
            typeIn.add(MenuTypeEnum.PROCESS_FORM.getType());
            params.put("typeIn", typeIn);
            params.put("enable", 1);
            params.put("appId", menuFormAddBatchDTO.getAppId());
            Integer count = paasMenuModel.getEntitysCount(params);
            if(count == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.MENU_NOT_EXIST);
            }
            List<PaasMenuEntity> menuList = new ArrayList<>();
            for (MenuFormAddDTO item : menuFormAddBatchDTO.getMenuList()) {
                long now = DateUtil.getInt();
                PaasMenuEntity paasMenuEntity = new PaasMenuEntity();
                BeanUtil.copyProperties(item, paasMenuEntity);
                paasMenuEntity.setCorpid(corpid);
                count = count + 1;
                paasMenuEntity.setSort(count);
                paasMenuEntity.setCreatorId(menuFormAddBatchDTO.getUserId());
                paasMenuEntity.setAddTime(now);
                paasMenuEntity.setUpdateTime(now);
                if (Objects.isNull(paasMenuEntity.getDistributorMark())) {
                    paasMenuEntity.setDistributorMark(DistributorMarkEnum.OTHER.getCode());
                }
                //待系列菜单新增公司级配置判断--标准版不可用待系列菜单（待出库，待入库，以销定购，智能补货）
                if(Objects.equals(paasMenuEntity.getAlias(), JXCMenuEnum.WAIT_INSTOCK.getAlias()) || Objects.equals(paasMenuEntity.getAlias(), JXCMenuEnum.WAIT_OUTSTOCK.getAlias())||
                        Objects.equals(paasMenuEntity.getAlias(), JXCMenuEnum.WAIT_PURCHASE.getAlias()) || Objects.equals(paasMenuEntity.getAlias(), JXCMenuEnum.SMART_REPLENISHMENT.getAlias())){
                    //设置顺序为0
                    paasMenuEntity.setSort(0);
                }
                menuList.add(paasMenuEntity);
            }
            List<PaasMenuEntity> resultList = new ArrayList<>();
            if (CollectionsUtil.isNotEmpty(menuList)) {
                resultList = paasMenuModel.insertBatch(menuList);
                if(Objects.isNull(resultList)){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
                }
            }

            menuFormAddBatchVO.setMenuList(resultList);
            String userName = menuFormAddBatchDTO.getLoginUserName();
            // 记录日志
            if (Objects.equals(menuFormAddBatchDTO.getCompanyInit(), 0)) {
                String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_ADD_MENU), userName, "");
                mongoLogHelp.buildLog(corpid, menuFormAddBatchDTO.getUserId(), userName, OperateModuleTypeEnum.PAAS_MENU, OperateTypeEnum.NEW, "", "", memo, menuFormAddBatchDTO.getHttpHeader());
            }
        } catch (XbbException e) {
            throw e;
        }  catch (Exception e) {
            LOG.error("menu数据库批量插入菜单出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return menuFormAddBatchVO;
    }
}
